$(function () {

    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var clazz_datagrid =  $('#clazz_datagrid');

    //数据表格
    clazz_datagrid.datagrid({

        url:'/clazz/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        //自适应表格
        fitColumns:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'cb',checkbox:true},
            {field:'id',hidden:true,width:100},
            {field:'name',title:'班级名称',width:100},
            {field:'teacher',title:'班主任',width:100,formatter:function (value,row,index) {

                return value?value.username:"";

                }},
            {field:'state',title:'班级状态',width:100, formatter:function (value,row,index) {

                 if (value==1){

                     return "<span style='color: #4cae4c'>毕业</span>";
                 }

                 if (value ==0){

                     return "<span style='color:red'>未毕业</span>";
                 }
                    }}
        ]],
        //点击的时候触发
        onClickRow:function (index,row) {

            if (row.state ==1){

                $("#start").linkbutton({

                    text:'启用'

                })

            }
            if (row.state ==0){

                $("#start").linkbutton({

                    text:'未启用'
                })
            }


        }
    });
    //end


    var cmdObj = {

        //状态的改变
        start:function(){

            //获取当前的操作的数据
            var row = clazz_datagrid.datagrid("getSelected");
            //判断是否选中数据
          if (!row){

              $.messager.alert("温馨提示","请先选择一条操作的数据");
              return;
          }
          $.messager.confirm("温馨提示","你确定要修改当前数据的状态吗?",function (r) {

              if (r){

                  $.get("/clazz/start.do?id="+row.id,function (data) {

                      if (data.success){

                          $.messager.confirm("温馨提示","状态改变",function (r) {

                           edit_dialog.dialog("close");
                           clazz_datagrid.datagrid("load");

                          });

                      }else{

                          $.messager.alert("温馨提示",data.msg,"error");
                      }

                  })
              }
          })


        },
        //添加按钮添加班级
        add : function () {

            edit_dialog.dialog("open");
            edit_dialog.dialog("setTitle","添加班级");
            edit_form.form("clear");

        },
        //编辑按钮编辑班级
        edit : function () {

            var row = clazz_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的班级","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑班级");
            //老师回显
            if (row.teacher){

                row["teacher.id"] = row.teacher.id;
            }
            //在读取数据之前,清除缓存
            edit_form.form("clear");

            edit_form.form("load",row);

            edit_dialog.dialog("open");

        },

        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#deptId").val();

            if(id){
                url = "/clazz/update.do";
            }else{
                url = "/clazz/save.do";
            }


            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            clazz_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },
        //删除班级
        remove : function () {
            var row = clazz_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的班级","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/clazz/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                clazz_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //刷新
        reload :function () {
            clazz_datagrid.datagrid("reload");
        },
        //高级查询
        searchT:function () {
        var kw = $("#keyword").val();
        //加载列表式,携带额外的参数
        $("#clazz_datagrid").datagrid("load",{
            keyword:kw

        })
    }

    }

    //抽取优化
    $("a").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end


    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: 350,
        height: 400,
    });
    //end

})//end













